import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/bezier_bounce_footer.dart';
import 'package:flutter_easyrefresh/bezier_hour_glass_header.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:village_android/component/grid_collection.dart';
import 'package:village_android/component/home_title_button.dart';
import 'package:village_android/component/news_collection.dart';
import 'package:village_android/component/swpier_widget.dart';
import 'package:village_android/config/service_method.dart';
import 'package:village_android/model/menu_model.dart';
import 'package:village_android/pages/article/article_list.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin {
  bool showEmpty = true;

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    print("加载首页...");
  }

  Widget initSingleChildScrollView(bannerList) {
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          SwiperWidget(items: bannerList),
          Visibility(
            visible: showEmpty,
            child: SizedBox(height: ScreenUtil.getInstance().setHeight(10)),
          ),
          GirdCollection(type: "index"),
          FlatButton(
            child: SizedBox(
              width: double.infinity,
              child: Row(children: <Widget>[
                Icon(
                  Icons.email,
                  color: Colors.white,
                ),
                Text(
                  ' 点击查看政策通知',
                  textAlign: TextAlign.left,
                  style: TextStyle(color: Colors.white),
                ),
              ]),
            ),
            onPressed: () {
              MenuModel menuModel = MenuModel("政策通知", 112, 11, "");
              var page = new ArticleListPage(
                menuModel: menuModel,
              );
              Navigator.push(
                  context, new MaterialPageRoute(builder: (context) => page));
            },
            color: Colors.red,
          ),
          HomeTitleButton(
            callBack: () {},
            text: "公共通知",
            module: 11,
            type: 111,
            color: Colors.red,
          ),
          NewsCollectionPage(
            title: "公共通知",
            module: 11,
            type: 111,
          ),
          HomeTitleButton(
            callBack: () {},
            text: "党建新闻",
            module: 1,
            type: 0,
            color: Colors.red,
          ),
          NewsCollectionPage(
            title: "党建新闻",
            module: 1,
            type: 0,
          ),
          HomeTitleButton(
            callBack: () {},
            text: "村务新闻",
            module: 2,
            type: 0,
            color: Colors.red,
          ),
          NewsCollectionPage(
            title: "村务新闻",
            module: 2,
            type: 0,
          ),
          HomeTitleButton(
            callBack: () {},
            text: "北亭监察",
            module: 9,
            type: 0,
            color: Colors.red,
          ),
          NewsCollectionPage(
            title: "北亭监察",
            module: 9,
            type: 0,
          ),
          HomeTitleButton(
            callBack: () {},
            text: "卫生通告",
            module: 3,
            type: 39,
            color: Colors.red,
          ),
          NewsCollectionPage(
            title: "卫生通告",
            module: 3,
            type: 39,
          ),
          SizedBox(height: 50.0),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("智慧党建 阳光北亭"),
      ),
      body: FutureBuilder(
        future: getBannerList(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            var data = json.decode(snapshot.data.toString());
            List<Map> bannerList = (data['data'] as List).cast();

            return EasyRefresh(
              header: BezierHourGlassHeader(backgroundColor: Colors.red),
              footer: BezierBounceFooter(backgroundColor: Colors.red),
              child: initSingleChildScrollView(bannerList),
              onRefresh: () async {
                getBannerList();
              },
              onLoad: () async {},
            );
          } else {
            return EasyRefresh(
              header: BezierHourGlassHeader(backgroundColor: Colors.red),
              footer: BezierBounceFooter(backgroundColor: Colors.red),
              child: initSingleChildScrollView(null),
              onRefresh: () async {
                getBannerList();
              },
              onLoad: () async {},
            );
          }
        },
      ),
    );
  }
}
